package com.meritdata.cloud.data.util;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

public class Base64Util {

    private static final Base64.Decoder decoder = Base64.getDecoder();
    private static final Base64.Encoder encoder = Base64.getEncoder();

    public static String encode(String sourceText) throws UnsupportedEncodingException {
        final byte[] textByte = sourceText.getBytes("UTF-8");
        return encoder.encodeToString(textByte);
    }

    public static String decode(String encodedText) throws UnsupportedEncodingException {
        return new String(decoder.decode(encodedText), "UTF-8");
    }

    public static void main(String[] args) {
        String text = "sukui苏魁";
        try {
            String encodedText = Base64Util.encode(text);
            System.out.println(encodedText);
            System.out.println(Base64Util.decode(encodedText));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
